<?php

/**
 * commentaire actions.
 *
 * @package    sinc
 * @subpackage commentaire
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class commentaireActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->id_article = $request->getParameter('id_article');
    $this->id_blog = $request->getParameter('id_blog');

    $c = new Criteria();
    $c->add(CommentairePeer::ID_ARTICLE,$this->id_article);

    $pager = new sfPropelPager('Commentaire',5);
    $pager->setCriteria($c);
    $pager->setPage($this->getRequestParameter('page', 1));
    $pager->init();
    $this->pager = $pager;

   $this->id_user = $this->getUser()->getGuardUser()->getId();

   $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->id_user);
   $this->css = PagePeer::doSelect($c);

   // $this->commentaire_list = CommentairePeer::doSelect($c);
  }

  public function executeNew(sfWebRequest $request)
  {
   $this->id_article = $request->getParameter('id_article');
   $this->auteur = $this->getUser()->getGuardUser()->getUsername();
   $this->id_blog = $request->getParameter('id_blog');


    $commentaire = new Commentaire();
    $commentaire->setIdArticle($this->id_article);
    $commentaire->setAuteur($this->auteur);
    $commentaire->setDate(date('Y-m-d'));

    $this->form = new CommentaireForm($commentaire);

     $this->id_user = $this->getUser()->getGuardUser()->getId();

   $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->id_user);
   $this->css = PagePeer::doSelect($c);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->id_article = $request->getParameter('id_article');
    $this->auteur = $this->getUser()->getGuardUser()->getUsername();
    $this->id_blog = $request->getParameter('id_blog');

    $comm = new Commentaire();
    $comm->setIdArticle($this->id_article);
    $comm->setAuteur($this->auteur);
    $comm->setDate(date('Y-m-d'));

    $this->form = new CommentaireForm($comm);

    $res = $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($commentaire = CommentairePeer::retrieveByPk($request->getParameter('id_commentaire')), sprintf('Object commentaire does not exist (%s).', $request->getParameter('id_commentaire')));
    $this->form = new CommentaireForm($commentaire);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($commentaire = CommentairePeer::retrieveByPk($request->getParameter('id_commentaire')), sprintf('Object commentaire does not exist (%s).', $request->getParameter('id_commentaire')));
    $this->form = new CommentaireForm($commentaire);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($commentaire = CommentairePeer::retrieveByPk($request->getParameter('id_commentaire')), sprintf('Object commentaire does not exist (%s).', $request->getParameter('id_commentaire')));
    $commentaire->delete();

    $this->redirect('commentaire/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $commentaires = $form->save();
        $this->id_blog = $request->getParameter('id_blog');
      $this->redirect('article/index?id_blog='.$this->id_blog);
    }
  }
}
